package com.shangxia.monomer.study.designPatterns.behavioralType.command;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: monomer
 * @ClassName: CommandClientSed
 * @Time: 2021/8/8
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class CommandClientSed {
    public static void main(String[] args) {
        Waiter waiter = new Waiter();
        Barbecuer barbecuer = new Barbecuer();
        waiter.addOrder(new ChickenWingCommand2(barbecuer));
        waiter.addOrder(new ChickenWingCommand2(barbecuer));
        MuttonCommand2 muttonCommand2 = new MuttonCommand2(barbecuer);
        waiter.addOrder(muttonCommand2);
        waiter.addOrder(new MuttonCommand2(barbecuer));
        waiter.removeOrder(muttonCommand2);
        waiter.notifyBarbecuer();
    }

}

@Data
class Barbecuer {
    private Integer muttonCount = 10;
    private Integer chickenWingCount = 10;

    public void bakeMutton() {
        System.out.println("烤牛肉");
    }

    public void bakeChickenWing() {
        System.out.println("烤鸡翅");
    }
}

@AllArgsConstructor
@Data
abstract class Command2 {
    public Barbecuer barbecuer;

    void execute() {
    }
}

class MuttonCommand2 extends Command2 {

    public MuttonCommand2(Barbecuer barbecuer) {
        super(barbecuer);
    }

    @Override
    void execute() {
        barbecuer.bakeMutton();
    }
}

class ChickenWingCommand2 extends Command2 {

    public ChickenWingCommand2(Barbecuer barbecuer) {
        super(barbecuer);
    }

    @Override
    void execute() {
        barbecuer.bakeChickenWing();
    }
}

class Waiter {
    private List<Command2> list = new ArrayList<>();

    public void addOrder(Command2 command2) {
        if (command2 instanceof MuttonCommand2) {
            if (command2.getBarbecuer().getMuttonCount() == 0) {
                System.out.println("牛肉没有了");
                return;
            }
        }
        if (command2 instanceof ChickenWingCommand2) {
            if (command2.getBarbecuer().getChickenWingCount() == 0) {
                System.out.println("鸡翅没有了");
            }
        }
        list.add(command2);
    }

    public void removeOrder(Command2 command2) {
        if (command2 instanceof MuttonCommand2) {
            command2.getBarbecuer().setMuttonCount(command2.getBarbecuer().getMuttonCount() + 1);
        }
        if (command2 instanceof ChickenWingCommand2) {
            command2.getBarbecuer().setChickenWingCount(command2.getBarbecuer().getChickenWingCount() + 1);
        }
        System.out.println("退单");
        list.remove(command2);
    }

    public void notifyBarbecuer() {
        for (Command2 command2 : list) {
            command2.execute();
        }
    }
}

